//
// Created by dongbo on 3/16/19.
//

#ifndef P_PAXOS_LEARNER_XY_H
#define P_PAXOS_LEARNER_XY_H

#include <iostream>
#include <sstream>

struct t_paxos_msg {
    enum msg_xy {
        lease_msg,
        ordinary_msg,
        distribute_msg,
    };

    struct lease {
        int xyid;
        int id;
        int time_p;

        void set_msg(int _id, int _time_p) {
            xyid = msg_xy::lease_msg;
            id = _id;
            time_p = _time_p;
        }

        std::string to_string() {
            std::ostringstream oss;
            oss << xyid << " " << id << " " << time_p;
            return oss.str();
        }

        void to_struct(std::istringstream &iss) {
            std::string dbug_str = iss.str();
            xyid = lease_msg;
            iss >> id;
            iss >> time_p;
        }
    };


    struct ordinary {
        int xyid;
        int nGroupID;
        int nAreaID;
        std::string strTopic;
        std::string tableName;
        std::string strContent;
        int content_len;

        void set_msg(int _nGroupID,
                     int _nAreaID,
                     std::string _strTopic,
                     std::string _tableName,
                     std::string _strContent) {
            xyid = msg_xy::ordinary_msg;
            nGroupID = _nGroupID;
            nAreaID = _nAreaID;
            strTopic = _strTopic;
            tableName = _tableName;
            strContent = _strContent;
            content_len = _strContent.length();
        }

        std::string to_string() {
            std::ostringstream oss;
            oss << xyid << " " << nGroupID << " " << nAreaID
                << " " << strTopic << " " << tableName
                << " " << strContent;
            return oss.str();
        }

        void to_struct(std::istringstream &iss) {
            xyid = ordinary_msg;
            std::string dbug_str = iss.str();
            iss >> nGroupID;
            iss >> nAreaID;
            iss >> strTopic;
            iss >> tableName;
            iss >> content_len;
            char tmp[4096] = {0};
            iss.getline(tmp, content_len > 4096 ? 4095 : content_len + 1);
            strContent.assign(tmp);
        }
    };

    struct distribute {
        int xyid;
        int id;
        std::string strTopic;
        int topic_gid;


        void set_msg(int _id,
                     std::string _strTopic,
                     int _topic_id) {
            xyid = msg_xy::ordinary_msg;
            id = _id;
            strTopic = _strTopic;
            topic_gid = _topic_id;
        }

        std::string to_string() {
            std::ostringstream oss;
            oss << xyid
                << " " << id
                << " " << strTopic
                << " " << topic_gid;
            return oss.str();
        }

        void to_struct(std::istringstream &iss) {
            xyid = distribute_msg;
            std::string dbug_str = iss.str();
            iss >> id;
            iss >> strTopic;
            iss >> topic_gid;
        }
    };
};


#endif //P_PAXOS_LEARNER_XY_H
